﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace Web.SystemConfig.SysCache
{
    public class ArchiveCacheManager
    {
        private static ArchiveCacheManager m_ArchiveCacheManager = null;
        private CacheManager m_CacheManager = null;

        ///使用缓存，保存一些数据库信息
        public ArchiveCacheManager()
        {
            if (this.m_CacheManager == null)
            {
                this.m_CacheManager = (CacheManager)CacheFactory.GetCacheManager();
            }
        }

        /// <summary>
        /// 实例化ArchiveCacheManager
        /// </summary>
        /// <returns></returns>
        public static ArchiveCacheManager GetInstance()
        {
            if (m_ArchiveCacheManager == null)
            {
                m_ArchiveCacheManager = new ArchiveCacheManager();
            }

            return m_ArchiveCacheManager;
        }

        /// <summary>
        /// 新增缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="refreshAction"></param>
        public void Add(string key, object value, ICacheItemRefreshAction refreshAction)
        {
            this.m_CacheManager.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromHours(1)));
        }

        /// <summary>
        /// 新增缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            this.m_CacheManager.Add(key, value);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            return this.m_CacheManager.GetData(key);
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            this.m_CacheManager.Remove(key);
        }

        /// <summary>
        /// 清空缓存
        /// </summary>
        public void Flush()
        {
            this.m_CacheManager.Flush();
        }
    }
}